<?php


namespace app\api\model;


use app\common\library\UserToken;
use app\common\model\BaseModel;
use hiredis;
use common;
use ApiOper;

class StaffModel extends BaseModel
{
    protected $name = 'staff';
    protected $pk = 'id';

    function STAFF_ID($city_id)
    {
        $key = 'STAFF_ID|' . $city_id;

        $id = hiredis::db()->incr($key);
        if ($id == 1) {

            $st = "select max(id) as maxid  from tb_staff where id < 'a'";
            $beans = [];
            hisql::bound_arg('city_id', $city_id, $st, $beans);
            $result = hisql::db()->query($st, $beans);
            $rows = $result->fetchAll(Phalcon\Db::FETCH_ASSOC);

            if ($rows[0]['maxid'] === NULL) {
                $id = $city_id . "0001";
            } else {
                $id = strval(intval($rows[0]['maxid']) + 1);
            }
            hiredis::db()->set($key, $id);
        }

        return strval($id);
    }

    function query_list($city_id, $condition, $pos, $limit)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'idcno' => 'string',
            'state' => 'int',
            'last_login_time' => 'string',
            'cc_role_id' => 'string',
            'cc_role_name' => 'string',
            'ops_role_id' => 'string',
            'ops_role_name' => 'string',
            'phone' => 'string',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
            'update_time' => 'string',
        ];


        $beans = [
            'city_id'=>$city_id,
            'is_deleted' => 0,
        ];

        if (isset($condition['time_interval_type']) && in_array($condition['time_interval_type'], [1, 2])) {
            if ($condition['time_interval_type'] == 1) {
                if (isset($condition['start_time'])) {
                    $beans['create_time'] = ['>=',$condition['start_time']];
                }
                if (isset($condition['end_time'])) {
                    $beans['create_time'] = ['<=',$condition['end_time']];
                }
            } else if ($condition['time_interval_type'] == 2) {
                if (isset($condition['start_time'])) {
                    $beans['last_order_time'] = ['>=',$condition['start_time']];
                }
                if (isset($condition['start_time'])) {
                    $beans['last_order_time'] = ['<=',$condition['end_time']];
                }
            }
        }
        if (!empty($condition['sk'])) {
            if (common::is_phone($condition['sk'])) {
                $beans['phone'] = $condition['sk'];
            } else if (common::is_name($condition['sk'])) {
                $beans['name'] = $condition['sk'];
            }
        }

        if (isset($condition['state'])) {
            $beans['state'] = $condition['state'];
        }

        if (isset($condition['cc_role_id'])) {
            if($condition['cc_role_id'] == "0"){
                $beans['cc_role_id'] = '';
            }
            else{
                $beans['cc_role_id'] = $condition['cc_role_id'];
            }

        }
        if (isset($condition['ops_role_id'])) {
            if($condition['ops_role_id'] == "0"){
                $beans['ops_role_id'] = '';
            }
            else{
                $beans['ops_role_id'] = $condition['ops_role_id'];
            }
        }
        if (isset($condition['cs_role_id'])) {
            if($condition['cs_role_id'] == "0"){
                $beans['cs_role_id'] = '';
            }
            else{
                $beans['cs_role_id'] = $condition['cs_role_id'];
            }
        }

        $rows = $this->where($beans)->field(common::array_keys($selectlist))->limit($pos,$limit)->select();
        $l = [];
        if (!empty($rows)){
            $l = $rows->toArray();
        }
        return $l;
    }

    function cs_query_list($city_id, $condition, $pos, $limit)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'idcno' => 'string',
            'state' => 'int',
            'last_login_time' => 'string',
            'cs_role_id' => 'string',
            'cs_role_name' => 'string',
            'phone' => 'string',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
            'update_time' => 'string',
        ];

        $beans=[
            'cc_role_id' => ['=',null],
            'cs_role_id' => ['<>',null],
            'is_deleted' => 0,
        ];

        if (isset($condition['time_interval_type']) && in_array($condition['time_interval_type'], [1, 2])) {
            if ($condition['time_interval_type'] == 1) {
                if (isset($condition['start_time'])) {
                    $beans['create_time'] = ['>=',$condition['start_time']];
                }
                if (isset($condition['end_time'])) {
                    $beans['create_time'] = ['<=',$condition['end_time']];
                }
            } else if ($condition['time_interval_type'] == 2) {
                if (isset($condition['start_time'])) {
                    $beans['last_order_time'] = ['>=',$condition['start_time']];
                }
                if (isset($condition['start_time'])) {
                    $beans['last_order_time'] = ['<=',$condition['end_time']];
                }
            }
        }

        if (!empty($condition['sk'])) {
            if (common::is_phone($condition['sk'])) {
                $beans['phone'] = $condition['sk'];
            } else if (common::is_name($condition['sk'])) {
                $beans['name'] = $condition['sk'];
            }
        }

        if (isset($condition['state'])) {
            $beans['state'] = $condition['state'];
        }

        if (isset($condition['cs_role_id'])) {
            if($condition['cs_role_id'] == "0"){
                $beans['cs_role_id'] = '';
            }
            else{
                $beans['cs_role_id'] = $condition['cs_role_id'];
            }
        }

        $rows = $this->where($beans)->field(common::array_keys($selectlist))->limit($pos,$limit)->select();
        $l = [];
        if (!empty($rows)){
            $l = $rows->toArray();
        }
        return $l;
    }

    function add($info)
    {
        $paramlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'idcno' => 'string',
            'state' => 'int',
            'last_login_time' => 'string',
            'cc_role_id' => 'string',
            'cc_role_name' => 'string',
            'ops_role_id' => 'string',
            'ops_role_name' => 'string',
            'cs_role_id' => 'string',
            'cs_role_name' => 'string',
            'phone' => 'string',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
            'update_time' => 'string',
        ];

        $data = common::type_conver($info, $paramlist);

        $data['id'] = $this->STAFF_ID($info['city_id']);

        ApiOper::instance()->append_creater_info($data);

        //hisql::insert('tb_staff', $data);

        self::create($data,true);

        return $data;
    }

    function query_info($city_id, $id)
    {
        $selectlist = [
            'id' => 'string',
            'name' => 'string',
            'idcno' => 'string',
            'phone' => 'string',
            'state' => 'string',
            'last_login_time' => 'string',
            'passwd' => 'string',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
            'update_time' => 'string',
            'is_deleted' => 'string',
            'cc_role_id' => 'string',
            'cc_role_name' => 'string',
            'ops_role_id' => 'string',
            'ops_role_name' => 'string',
            'city_id' => 'string',
            'wxmp_open_id' => 'string',
            'wx_union_id' => 'string',
            'wx_nickname' => 'string',
            'wx_gender' => 'string',
            'wx_city' => 'string',
            'wx_province' => 'string',
            'wx_avatar_url' => 'string',
            'web_wx_union_id' => 'string',
        ];

        $row = $this->where('id',$id)->field(common::array_keys($selectlist))->find();
        if (empty($row)) {
            return null;
        }
        return $row;
    }

    function query_info_by_phone($city_id, $phone)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'idcno' => 'string',
            'phone' => 'string',
            'state' => 'string',
            'last_login_time' => 'string',
            'passwd' => 'string',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
            'update_time' => 'string',
            'is_deleted' => 'int',
            'cc_role_id' => 'string',
            'cc_role_name' => 'string',
            'ops_role_id' => 'string',
            'ops_role_name' => 'string',
            'wxmp_open_id' => 'string',
            'wx_union_id' => 'string',
            'wx_nickname' => 'string',
            'wx_gender' => 'string',
            'wx_city' => 'string',
            'wx_province' => 'string',
            'wx_avatar_url' => 'string',
            'web_wx_union_id' => 'string',
        ];

        $beans = [
            'is_deleted' => 0,
            'phone' => $phone,
        ];

        $rows = $this->where($beans)->where('city_id', ['=',null],['=',$city_id],'or')->field(common::array_keys($selectlist))->select();

        if (empty($rows)) {
            return null;
        }

        return $rows[0];
    }

    function query_info_by_web_union_id($city_id, $union_id)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'idcno' => 'string',
            'phone' => 'string',
            'state' => 'string',
            'last_login_time' => 'string',
            'passwd' => 'string',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
            'update_time' => 'string',
            'is_deleted' => 'int',
            'cc_role_id' => 'string',
            'cc_role_name' => 'string',
            'ops_role_id' => 'string',
            'ops_role_name' => 'string',
            'wxmp_open_id' => 'string',
            'wx_union_id' => 'string',
            'wx_nickname' => 'string',
            'wx_gender' => 'string',
            'wx_city' => 'string',
            'wx_province' => 'string',
            'wx_avatar_url' => 'string',
            'web_wx_union_id' => 'string',
        ];

        $beans = [
            'is_deleted' => 0,
            'web_wx_union_id' => $union_id,
        ];

        $rows = $this->where($beans)->where('city_id', ['=',null],['=',$city_id],'or')->field(common::array_keys($selectlist))->select();

        if (empty($rows)) {
            return null;
        }

        return $rows[0];
    }

    function query_list_by_phone($phone)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'idcno' => 'string',
            'phone' => 'string',
            'state' => 'string',
            'last_login_time' => 'string',
            'passwd' => 'string',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
            'update_time' => 'string',
            'is_deleted' => 'string',
            'cc_role_id' => 'string',
            'cc_role_name' => 'string',
            'ops_role_id' => 'string',
            'ops_role_name' => 'string',
            'cs_role_id' => 'string',
            'cs_role_name' => 'string',
            'wxmp_open_id' => 'string',
            'wx_union_id' => 'string',
            'wx_nickname' => 'string',
            'wx_gender' => 'string',
            'wx_city' => 'string',
            'wx_province' => 'string',
            'wx_avatar_url' => 'string',
            'web_wx_union_id' => 'string',
        ];

        $beans = [
            'phone'=>$phone,
            'is_deleted' => 0
        ];

        $rows = $this->where($beans)->field(common::array_keys($selectlist))->order('last_login_time','desc')->select();
        if (empty($rows)) {
            return null;
        }
        return $rows->toArray();
    }

    function query_list_by_web_union_id($union_id)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'idcno' => 'string',
            'phone' => 'string',
            'state' => 'string',
            'last_login_time' => 'string',
            'passwd' => 'string',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
            'update_time' => 'string',
            'is_deleted' => 'string',
            'cc_role_id' => 'string',
            'cc_role_name' => 'string',
            'ops_role_id' => 'string',
            'ops_role_name' => 'string',
            'cs_role_id' => 'string',
            'cs_role_name' => 'string',
            'wxmp_open_id' => 'string',
            'wx_union_id' => 'string',
            'wx_nickname' => 'string',
            'wx_gender' => 'string',
            'wx_city' => 'string',
            'wx_province' => 'string',
            'wx_avatar_url' => 'string',
            'web_wx_union_id' => 'string',
        ];

        $beans = [
            'web_wx_union_id'=>$union_id,
            'is_deleted' => 0
        ];

        $rows = $this->where($beans)->field(common::array_keys($selectlist))->order('last_login_time','desc')->select();
        if (empty($rows)) {
            return null;
        }
        return $rows->toArray();
    }

    function query_list_by_passwd($phone, $passwd)
    {
        $selectlist = [
            'id' => 'string',
            'city_id' => 'string',
            'name' => 'string',
            'idcno' => 'string',
            'phone' => 'string',
            'state' => 'string',
            'last_login_time' => 'string',
            'passwd' => 'string',
            'creator_id' => 'string',
            'creator_name' => 'string',
            'create_time' => 'string',
            'updater_id' => 'string',
            'updater_name' => 'string',
            'update_time' => 'string',
            'is_deleted' => 'string',
            'cc_role_id' => 'string',
            'cs_role_id' => 'string',
            'cc_role_name' => 'string',
            'ops_role_id' => 'string',
            'ops_role_name' => 'string',
            'wxmp_open_id' => 'string',
            'wx_union_id' => 'string',
            'wx_nickname' => 'string',
            'wx_gender' => 'string',
            'wx_city' => 'string',
            'wx_province' => 'string',
            'wx_avatar_url' => 'string',
            'web_wx_union_id' => 'string',
        ];

        $beans = [
            'phone'=>$phone,
            'passwd'=>$passwd,
            'is_deleted' => 0,
        ];

        $rows = $this->where($beans)->field(common::array_keys($selectlist))->order('last_login_time','desc')->select();
        if (empty($rows)) {
            return null;
        }
        return $rows->toArray();
    }

    function update_staff($city_id, $id, $info)
    {
        $paramlist = [
            'cc_role_id' => 'string',
            'cc_role_name' => 'string',
            'ops_role_id' => 'string',
            'ops_role_name' => 'string',
            'state' => 'int',
            'wx_union_id' => 'string',
            'wxmp_open_id' => 'string',
            'wx_nickname' => 'string',
            'wx_city' => 'string',
            'wx_province' => 'string',
            'wx_avatar_url' => 'string',
            'wx_gender' => 'string',
            'last_login_time' => 'string',
        ];

        $data = common::type_conver($info, $paramlist);
        ApiOper::instance()->append_updater_info($data);
        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];

        self::update($data, $where);

        return $data;
    }

    function reset_passwd($city_id, $phone, $new_passwd)
    {
        $data = [
            'passwd' => $new_passwd,
        ];

        ApiOper::instance()->append_updater_info($data);
        $where = [
            //'city_id' => $city_id,
            'phone' => $phone,
        ];

        self::update($data, $where);
    }

    function update_login_time($city_id, $id)
    {
        $data = [
            'last_login_time' => date('Y-m-d H:i:s'),
        ];

        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];

        self::update($data, $where);
    }

    function delete_staff($city_id, $id)
    {
        $data = [
            'is_deleted' => 1,
            'update_time' => date('Y-m-d H:i:s'),
        ];

        $where = [
            'city_id' => $city_id,
            'id' => $id,
        ];

        self::update( $data, $where);
        (new UserToken())->mgr_delete_all_token($id);
    }

    //function
    function update_union_id($phone,$union_id)
    {
        $data = [
            'web_wx_union_id' => $union_id,
        ];

        ApiOper::instance()->append_updater_info($data);
        $where = [
            //'city_id' => $city_id,
            'phone' => $phone,
        ];

        self::update($data, $where);
    }
}